package vn.edu.thptduongbachmai.model;

import com.vng.zing.common.HReqParam;
import com.vng.zing.logger.ZLogger;
import com.vng.zing.stats.Profiler;
import com.vng.zing.stats.ThreadProfiler;
import hapax.TemplateDataDictionary;
import vn.edu.thptduongbachmai.common.Auth;
import vn.edu.thptduongbachmai.common.Configuration;
import vn.edu.thptduongbachmai.common.TemplateManager;
import vn.edu.thptduongbachmai.common.Utilities;
import vn.edu.thptduongbachmai.model.data.UsersModel;
import vn.edu.thptduongbachmai.model.pojo.Users;
import vn.edu.thptduongbachmai.services.SessionService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import vn.edu.thptduongbachmai.model.BaseModel;

/**
 *
 * @author tiennv4
 */
public class SignInModel extends BaseModel {

	private static final Logger _Logger = ZLogger.getLogger(SignInModel.class);
	public static String FAIL = "The username or password you entered is incorrect.";
	public static String SUCCESS = "Successful";
//	public static final SignInModel Instance = new SignInModel();
//
//	private SignInModel() {
//	}
//
//	@Override
//	public void process(HttpServletRequest req, HttpServletResponse resp) {
//		ThreadProfiler profiler = Profiler.getThreadProfiler();
//		try {
//			String username = HReqParam.getString(req, "u", "");
//			String password = HReqParam.getString(req, "p", "");
//			boolean isLogged = false;
//
//			if (!username.isEmpty() && !password.isEmpty()) {
//				int userId = UsersModel.Instance.exist(username);
//				if (userId > 0) {
//					Users user = UsersModel.Instance.get(userId);
//					password = Utilities.sha256(password);
//					if (user.getPassword().equals(password)) {
//						Auth auth = new Auth(user);
//						String session = SessionService.Instance.put(auth);
//						if (session != null && !session.isEmpty()) {
//							isLogged = true;
//							req.setAttribute(Auth.AUTH_KEY, auth);
//							Utilities.setCookie(Configuration.APP_KEY_COOKIE, session, 60 * 5, false, "/", Configuration.APP_DOMAIN, req, resp);
//						}
//					}
//				}
//			}
//			renderPageJSON(req, resp, isLogged);
//
//		} catch (Exception ex) {
//			_Logger.error(ex.getMessage(), ex);
//		}
//	}
//
//	public void renderPageJSON(HttpServletRequest req, HttpServletResponse resp, boolean success) {
//		JSONObject ret = new JSONObject();
//		if (success) {
//			Auth auth = (Auth) req.getAttribute(Auth.AUTH_KEY);
//			ret.put("error", 0);
//			ret.put("message", SUCCESS);
//			ret.put("data", auth.toJSONObject());
//		} else {
//			ret.put("error", -1);
//			ret.put("message", FAIL);
//		}
//		TemplateManager.printJSON(ret.toJSONString(), resp);
//	}

    @Override
    public TemplateDataDictionary prepareLayout() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public TemplateDataDictionary prepareBody(TemplateDataDictionary dic) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   
    @Override
    public void render(TemplateDataDictionary dic, HttpServletResponse resq) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public TemplateDataDictionary pullData(TemplateDataDictionary dic, HttpServletRequest req) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    
}
